package com.hs.ph.Servlet;

import com.alibaba.fastjson.JSON;
import com.hs.ph.Dao.UserDao;
import com.hs.ph.Dao.impl.UserDaoImpl;
import com.hs.ph.Entity.Result;
import com.hs.ph.Entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        UserDao userDao = new UserDaoImpl();
        User user = userDao.selectByUsernameAndPwd(username, pwd);
        if (user==null){
            Result result = Result.error("登录失败");
            resp.getWriter().write(JSON.toJSONString(result));
        }else {
            Result result = Result.success("登录成功");
            resp.getWriter().write(JSON.toJSONString(result));
        }

    }
}
